package com.linwen.excise.leetcode;

import java.util.Arrays;

/**
 * @author : linwen
 * @date : 下午6:11 2021/4/4
 */
public class _136只出现一次的数字 {
    public static void main(String[] args) {
        int[] arr = new int[]{4,1,2,1,2};
        System.out.println(_136只出现一次的数字.singleNumber(arr));
        System.out.println(_136只出现一次的数字.singleNumber2(arr));
    }
    public static int singleNumber(int[] nums) {
        Arrays.sort(nums);
        for (int i = 0; i < nums.length-1; i=i+2) {
            if(nums[i]!=nums[i+1]){
                return nums[i];
            }
        }
        return nums[nums.length-1];
    }

    public static int singleNumber2(int[] nums) {
        int result = 0;
        for (int i : nums) {
            result = i ^ result;
        }
        return result;
    }
}
